﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using ScriptPacker.Core;
using System.IO;
using System.Text.RegularExpressions;
using System.Reflection;
using ScriptPacker.Core.Utils;

namespace ScriptPacker.UI
{
	public partial class fmHome
	{
		public List<string> JsFileList { get; set; }
		public ImageList SmallImgList { get; set; }
		private List<ListViewItem> _JsListItems;

		private Size curWinSize { get; set; }

		private static readonly List<Color> bgColors=new List<Color>{
			Color.Khaki,
			Color.LightPink
		};
		public List<ListViewItem> JsListItems {
			get
			{
				_JsListItems.Clear();
				var i = 0;
				JsFileList.ForEach(x =>
				{
					if (!_JsListItems.Exists(y => y.Name == x))
					{
						i++;
						var j = (i % 2) > 0 ? 1 : 0;
						_JsListItems.Add(new ListViewItem
						{
							BackColor = bgColors[j],
							Name = x,
							Text = x,
							ToolTipText = x.Substring(x.LastIndexOf('\\')),
							ImageKey="img.script"
						});
					}
				});
				return _JsListItems;
			}
		}





		public void LoadJSFiles() {
			this.lvFileList.Items.Clear();
			this.lvFileList.Items.AddRange(JsListItems.ToArray());
		}
		public void LoadEncodingData() {
			cbxEncodingList.Items.Add(ECMAScriptPacker.PackerEncoding.None);
			cbxEncodingList.Items.Add(ECMAScriptPacker.PackerEncoding.Numeric);
			cbxEncodingList.Items.Add(ECMAScriptPacker.PackerEncoding.Mid);
			cbxEncodingList.Items.Add(ECMAScriptPacker.PackerEncoding.Normal);
			cbxEncodingList.Items.Add(ECMAScriptPacker.PackerEncoding.HighAscii);
			cbxEncodingList.SelectedItem = ECMAScriptPacker.PackerEncoding.Normal;
		}
		public string GetPackedJSFileData() {
			ECMAScriptPacker p = new ECMAScriptPacker((ECMAScriptPacker.PackerEncoding)cbxEncodingList.SelectedItem, this.cbFastDecode.Checked, this.cbSpecialChar.Checked);
			var retVal = new StringBuilder();
			Regex regex = new Regex("([^\r])(\n+)");
			StreamReader s;
			JsFileList.ForEach(x => {
				if (!File.Exists(x)) return;
				s=File.OpenText(x);
				retVal.Append(p.Pack(regex.Replace(s.ReadToEnd(), new MatchEvaluator(changeUnixLineEndings)).Replace("\n","\r\n")));
				s.Dispose();
			});

			//pack
			return retVal.ToString();
		}

		public string GetStrippedFileData(bool isJS)
		{
			var retVal = new StringBuilder();
			JsFileList.ForEach(x => {
				if (!File.Exists(x)) return ;
				using (StreamReader s = File.OpenText(x))
				{
					retVal.Append(s.ReadToEnd());
				}
			});
			return retVal.ToString().StripWhitespace(isJS);
		}

		#region helper methods
		private static string changeUnixLineEndings(Match match)
		{
			return match.Value.Replace("\n", "\r\n");
		}
		private void loadImgList() {
			SmallImgList = new ImageList();
			
			Image script=Image.FromStream(this.GetType().GetEmbeddedFile("ScriptPacker.Assets.img.script_(add)_16x16.gif"));
			SmallImgList.Images.Add("img.script",script);
			this.lvFileList.SmallImageList = SmallImgList;
		}

		private void onWinResize(object sender, EventArgs e) {
			var fm = sender as Form;
			fm.ResizeWith(curWinSize.Height,curWinSize.Width,this.lvFileList, this.pnActions, this.pnBottom,this.groupBox1);
			curWinSize = fm.Size;
		}
		private static List<string> resolveJSFiles(IEnumerable<string> files)
		{
			var finalJsFiles = new List<string>();
			//jscfg file
			var jsCfgFiles = files.Where(x => x.ToLower().EndsWith(".jscfg"));

			var tempfiles = new List<string>();

			jsCfgFiles.ToList().ForEach(x => {
				XmlUtil.Deserialize<List<string>>(x, out tempfiles);
				if (null != tempfiles && tempfiles.Count > 0) {
					finalJsFiles.AddRange(tempfiles);
				}
			});

			//js file
			var jsfiles = files.Where(x => x.ToLower().EndsWith(".js"));
			if (null != jsfiles && jsfiles.Count() > 0) {
				finalJsFiles.AddRange(jsfiles);
			}

			return finalJsFiles;
		}
		#endregion

	}
}
